/*************************************************************************
	> File Name: dispose.c
	> Author: 
	> Mail: 
	> Created Time: 2018年08月28日 星期二 10时42分27秒
 ************************************************************************/

#include <dispose.h>

extern int zgbfd;
static int t;

//读取配置信息
/*
 * 函数名:read_config
 * 功能:读取配置信息
 * 参数:config *con:读取配置信息成功后,通过该结构体返回
 * 返回值:成功返回OK,失败返回ERR
 */
int read_config(config *con)
{
	read_config_data(con);
	//printf("max_tem=%f\tmax_hum=%f\tmax_lux=%f\n", con->max_env.tem, con->max_env.hum, con->max_env.lux);
	//printf("min_tem=%f\tmin_hum=%f\tmin_lux=%f\n", con->min_env.tem, con->min_env.hum, con->min_env.lux);
    return OK;
}

//数据处理
/*
 * 函数名:data_dispose
 * 功能:对数据进行处理,保证精确度,取10次数据的平均值
 * 参数:env_data *env:环境数据结构体指针,用来返回处理后的数据
 * 返回值:成功返回OK,失败返回ERR
 */
int data_dispose(server_to_client *stc, adm_state *adm_s, int fifo_fd, config *con)
{
	
	int ret = 0;
	int fd;
	int rfid = 0;
	int i = 0;
	env_data num;
	num.tem = 0;
	num.hum = 0;
	num.lux = 0;
	fifo *p = (fifo *)malloc(sizeof(fifo));
	env_dev_rfid_card *data = (env_dev_rfid_card *)malloc(N_LY * sizeof(env_dev_rfid_card));
	memset(data, 0, N_LY * sizeof(env_dev_rfid_card));
	for (i= 0; i < N_LY; i++)
	{
rfid_card :
		//从M0板采集数据
		while(1)
		{
			ret = collect_data(&data[i]);
			if (OK == ret)
			{
				break;
			}
		}
		
		if (data[i].rfid_card != 0)
		{
			if (data[i].rfid_card == -1)
			{
				rfid = -2;
				memset(p, 0, sizeof(fifo));
				memcpy(p->buf, FFCR, strlen(FFCR));
				memcpy(p->buf + strlen(FFCR), &rfid, sizeof(int));
				
				//向M0板发送数据
				fifo_fd = open(TOWARD_M0_FIFO, O_RDWR);
				ret = write(fifo_fd, &p->buf, BUF_SIZE - 1);
				close(fifo_fd);
				
				if (1 == adm_s->state)
				{
					//向客户端发送数据
					fifo_fd = open(SOCKET_FIFO, O_RDWR);
					ret = write(fifo_fd, p, BUF_SIZE - 1);
					close(fifo_fd);
				}
			}
			else
			{
				//printf("dispose$$$$$$$$$$shua ka xin xi:%d\n", data[i].rfid_card);
				memset(p, 0, sizeof(fifo));
				
				memcpy(p->buf, MJKH, strlen(MJKH));
				memcpy(p->buf + strlen(MJKH), &(data[i].rfid_card), sizeof(int));
				
				fd = open(SQLITE_FIFO, O_RDWR);
				write(fd, p, sizeof(fifo));
				close(fd);
				data[i].rfid_card = 0;
				//printf("dispose$$$$$$$$$$shua ka xin xi:%d\n", data[i].rfid_card);
			}
			goto rfid_card;
		}
		
		//管道数据接受
		ret = read_fifo_data(adm_s, fifo_fd, con);
		
		//向客户端发送设备状态数据		
		memset(p, 0, sizeof(fifo));
		memcpy(p->buf, DEVI, strlen(DEVI));
		memcpy(p->buf + strlen(DEVI), &data[i].dev, sizeof(dev));
		
		fd = open(SOCKET_FIFO, O_RDWR);
		write(fd, p, sizeof(fifo));
		close(fd);	
		//printf("dispose$$$$$$$$$$cai ji wan cheng %d\n", i);
	}
	for (i= 0; i < N_LY; i++)
	{
		num.tem += data[i].env_data.tem;
		num.hum += data[i].env_data.hum;
		num.lux += data[i].env_data.lux;
	}
	stc->env_data.tem = num.tem / N_LY;
	stc->env_data.hum = num.hum / N_LY;
	stc->env_data.lux = num.lux / N_LY;
	
	stc->dev.air_fan = data[N_LY-1].dev.air_fan;
	stc->dev.sunshade = data[N_LY-1].dev.sunshade;
	stc->dev.spray_water = data[N_LY-1].dev.spray_water;
	
	//printf("dispose$$$$$$$$$$data_dispose:\n");
	//printf("air_fan=%d\tsunshade=%d\tspray_water=%d\n", stc->dev.air_fan, stc->dev.sunshade, stc->dev.spray_water);
	
	free(p);
	return OK;
}

//数据判断
/*
 * 函数名:data_judge
 * 功能:对数据进行判断,是否需要改变设备状态等
 * 参数:server_to_client *stc:环境数据结构体指针,用来返回处理后的数据
 *		config *con:配置信息结构体
 * 		int *t:标志数据,用来标志是否需要改变设备状态,0表示不需要改变
 * 返回值:成功返回OK,失败返回ERR
 */
int data_judge(server_to_client *stc, config *con)
{
	//printf("max:tem=%d\thum=%d\tlux=%d\n", con->max_env.tem, con->max_env.hum, con->max_env.lux);
	//printf("min:tem=%d\thum=%d\tlux=%d\n", con->min_env.tem, con->min_env.hum, con->min_env.lux);
	int ret = 0;
	ret = jusge(stc->env_data.tem, con->max_env.tem, con->min_env.tem);
	if (LOW == ret)
	{
		stc->env_state.tem_state = LOW;
		if (0 == t && 0 != stc->dev.air_fan)
		{
			stc->dev.air_fan = 0;
			ret = zigbee_exe_cmd(zgbfd,"FAN_OFF");
		}
	}
	else if (HEIGHT == ret)
	{
		stc->env_state.tem_state = HEIGHT;
		if (0 == t && 1 != stc->dev.air_fan)
		{
			stc->dev.air_fan = 1;
			ret = zigbee_exe_cmd(zgbfd,"FAN_ON");
		}
	}
	else
	{
		stc->env_state.tem_state = NORMAL;
	}
	
	ret = jusge(stc->env_data.hum, con->max_env.hum, con->min_env.hum);
	if (LOW == ret)
	{
		stc->env_state.hum_state = LOW;
		if (0 == t && 1 != stc->dev.spray_water)
		{
			stc->dev.spray_water = 1;
			ret = zigbee_exe_cmd(zgbfd,"BUZZ_ON");
		}
	}
	else if (HEIGHT == ret)
	{
		stc->env_state.hum_state = HEIGHT;
		if (0 == t && 0 != stc->dev.spray_water)
		{
			stc->dev.spray_water = 0;
			ret = zigbee_exe_cmd(zgbfd,"BUZZ_OFF");
		}
	}
	else
	{
		stc->env_state.hum_state = NORMAL;
	}
	
	ret = jusge(stc->env_data.lux, con->max_env.lux, con->min_env.lux);
	if (LOW == ret)
	{
		stc->env_state.lux_state = LOW;
		if (0 == t && 0 != stc->dev.sunshade)
		{
			stc->dev.sunshade = 0;		
			ret = zigbee_exe_cmd(zgbfd,"LIGHT_OFF");
		}
	}
	else if (HEIGHT == ret)
	{
		stc->env_state.lux_state = HEIGHT;
		if (0 == t && 1 != stc->dev.sunshade)
		{
			stc->dev.sunshade = 1;			
			ret = zigbee_exe_cmd(zgbfd,"LIGHT_ON");
		}
	}
	else
	{
		stc->env_state.lux_state = NORMAL;
	}
	
	return OK;
}

//管道数据接受
/*
 * 函数名:read_fifo_data
 * 功能:接收管道内的数据
 * 参数:adm_state *adm_s:接收到的客户端登录状态变化数据
 * 返回值:成功返回OK,失败返回ERR
 */
int read_fifo_data(adm_state *adm_s, int fifo_fd, config *con)
{
	int ret = 0;
	int fd;
	fifo *p = (fifo *)malloc(sizeof(fifo));
	memset(p, 0, sizeof(fifo));
	ret = read(fifo_fd, p, sizeof(fifo));
	
	//printf("dispose$$$$$$$$$$ buf:%s\n", p->buf);
	if (0 == memcmp(p->buf, DLOK, strlen(DLOK)))	//登录反馈信息,如果登录成功
	{
		adm_s->state = 1;
		adm_s->sockID = p->sockID;
		
		memcpy(p->buf, PZXG, strlen(PZXG));
		memcpy(p->buf + strlen(PZXG), con, sizeof(config));
		//printf("deng lu fan kun");
		//printf("max_tem=%f\tmax_hum=%f\tmax_lux=%f\n", con->max_env.tem, con->max_env.hum, con->max_env.lux);
		//printf("min_tem=%f\tmin_hum=%f\tmin_lux=%f\n", con->min_env.tem, con->min_env.hum, con->min_env.lux);
		fd = open(SOCKET_FIFO, O_RDWR);
		ret = write(fd, p, sizeof(fifo));
		close(fd);
	}
	else if (0 == memcmp(p->buf, PZXG, strlen(PZXG)))	//修改配置信息
	{
		memcpy(con, p->buf + strlen(PZXG), sizeof(config));
		write_config_data(con);
		//printf("xiu gai pei zhi");
		//printf("max_tem=%f\tmax_hum=%f\tmax_lux=%f\n", con->max_env.tem, con->max_env.hum, con->max_env.lux);
		//printf("min_tem=%f\tmin_hum=%f\tmin_lux=%f\n", con->min_env.tem, con->min_env.hum, con->min_env.lux);
	}
	else if (0 == memcmp(p->buf, ZDKZ, strlen(ZDKZ)))
	{
		t = 0;
	}
	else if (0 == memcmp(p->buf, SDKZ, strlen(SDKZ)))
	{
		t = 1;
	}
	free(p);
	return OK;
}

//向M0板线程发送数据
/*
 * 函数名:write_M0_data
 * 功能:向M0板发送数据
 * 参数:dev dev:设备状态控制结构体
 * 返回值:成功返回OK,失败返回ERR
 */
int write_M0_data(env_dev_rfid_card edr)
{
	
	int ret = 0;
	int fifo_fd;
	char *buf = (char *)malloc(200);
	
	memset(buf, 0, 200);
	memcpy(buf, DEVI, strlen(DEVI));
	memcpy(buf + strlen(DEVI), &edr, sizeof(env_dev_rfid_card));
	//printf("dispose$$$$$$$$$$write_M0_data:\n");
	//printf("air_fan=%d\tsunshade=%d\tspray_water=%d\n", edr.dev.air_fan, edr.dev.sunshade, edr.dev.spray_water);
	fifo_fd = open(TOWARD_M0_FIFO, O_RDWR);
	write(fifo_fd, buf, sizeof(fifo));
	close(fifo_fd);
	free(buf);
	
	return OK;
}

//向数据库操作进程发送数据
/*
 * 函数名:write_sql_data
 * 功能:向数据库操作线程发送数据
 * 参数:server_to_client *stc:要保存的数据结构体
 * 返回值:成功返回OK,失败返回ERR
 */
int write_sql_data(server_to_client *stc)
{
	
	int ret = 0;
	int fifo_fd;
	fifo *p = (fifo *)malloc(sizeof(fifo));
	
	memset(p, 0, sizeof(fifo));
	memcpy(p->buf, ZTXX, strlen(ZTXX));
	memcpy(p->buf + strlen(ZTXX), stc, sizeof(server_to_client));
	
	fifo_fd = open(SQLITE_FIFO, O_RDWR);
	write(fifo_fd, p, sizeof(fifo));
	close(fifo_fd);
	free(p);
	
	return OK;
}

//向客户端发送数据
//向数据库操作进程发送数据
/*
 * 函数名:write_cli_data
 * 功能:向客户端发送数据
 * 参数:server_to_client *stc:要保存的数据结构体
 *		adm_state *adm_s:客户端登录状态
 * 返回值:成功返回OK,失败返回ERR
 */
int write_cli_data(server_to_client *stc, adm_state *adm_s)
{
	int ret = 0;
	int fifo_fd;
	fifo *p = (fifo *)malloc(sizeof(fifo));
	memset(p, 0, sizeof(fifo));
	
	p->sockID = adm_s->sockID;
	memcpy(p->buf, ZTXX, strlen(ZTXX));
	memcpy(p->buf + strlen(ZTXX), stc, sizeof(server_to_client));
	
	fifo_fd = open(SOCKET_FIFO, O_RDWR);
	write(fifo_fd, p, sizeof(fifo));
	close(fifo_fd);
	free(p);
	return OK;
}


//判断环境数据是否正常
int jusge(float tem, float max_tem, float min_tem)
{
	if (tem > max_tem)
	{
		return HEIGHT;
	}
	else if (tem < min_tem)
	{
		return LOW;
	}
	else
	{
		return NORMAL;
	}
}


